import ipcTips from './tips.vue'

let Tips = {};

//只有定义了install方法，才能使用Vue.use 进行引用
Tips.install = function (Vue,options){

    let _options = {
        delay:3000,
        type:'primary'
    }

    //参数覆盖
    for(let key in options){
        _options[key] = options[key]
    }


    



    let timer;

    const tips_main = {
        show:function(msg,opt){
            clearTimeout(timer)

            //参数覆盖
            let cb = '';

            if(typeof opt === 'object' && opt !== null){
                for(let key in opt){
                    _options[key] = opt[key]
                }
            }else if(typeof opt === 'function'){
                cb = opt;
            }

            const TipsController = Vue.extend(ipcTips)
            let instance = new TipsController();
            instance.$mount(document.createElement('div'));

            instance.msg = msg;
            instance.type = _options.type;
            instance.showFlag = true;

            document.body.appendChild(instance.$el);


            

            timer = setTimeout(() => {
                instance.showFlag = false;

                setTimeout(() => {
                    document.body.removeChild(instance.$el)
                    cb && cb()
                }, 500)
            },_options.delay)
        },
        success:function(msg){
            this.show(msg,{type:'success'})
        },
        error:function(msg){
            this.show(msg,{type:'error'})
        },
        warning:function(msg){
            this.show(msg,{type:'warning'})
        }
    } 

    //将方法挂载全局
    Vue.prototype.$ipcTips = tips_main;
}

/*if(typeof window !== 'undefined' && window.Vue){
    window.Vue.use(Tips)
}*/

export default Tips;